package com.kanshu.ksgb.fastread.doudou.module.makemoney.share;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;

import com.kanshu.ksgb.fastread.doudou.R;
import com.kanshu.ksgb.fastread.doudou.common.business.ad.presenter.AdPresenter;
import com.kanshu.ksgb.fastread.doudou.common.share.ShareBean;
import com.kanshu.ksgb.fastread.doudou.common.share.ShareUtils;
import com.kanshu.ksgb.fastread.doudou.common.util.DisplayUtils;
import com.kanshu.ksgb.fastread.doudou.module.makemoney.activity.Face2FaceActivity;
import com.mob.MobSDK;

import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;

/**
 * Created by wcy on 18/5/29.
 */

public class ShareDialog extends Dialog {
    private Activity mActivity;
    private ShareBean mShareBean;

    public ShareDialog(@NonNull Context context, ShareBean bean) {
        super(context, R.style.dialog_style);
        mActivity = (Activity) context;
        mShareBean = bean;
        init();
    }

    private void init() {
        setContentView(R.layout.dialog_share_layout);
        ButterKnife.bind(this);
        // 分享初始化
        MobSDK.init(mActivity, mActivity.getString(R.string.mob_app_appkey), mActivity.getString(R.string.mob_app_secret));
    }

    public static ShareDialog show(Activity activity, final ShareBean bean) {
        if (activity == null || activity.isFinishing()) {
            return null;
        }
        ShareDialog dlg = new ShareDialog(activity, bean);
        dlg.show();
        WindowManager.LayoutParams layoutParams = dlg.getWindow().getAttributes();
        dlg.getWindow().setWindowAnimations(R.style.AnimBottom);
        layoutParams.width = DisplayUtils.getScreenWidth(activity);
        layoutParams.gravity = Gravity.BOTTOM;
        dlg.getWindow().setAttributes(layoutParams);
        return dlg;
    }

    @OnClick({R.id.wechat, R.id.friend, R.id.face, R.id.cancel})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.wechat:
                if (mShareBean == null) {
                    return;
                }
                mShareBean.platform = Wechat.NAME;
                ShareUtils.share(mActivity, mShareBean, this);
                AdPresenter.Companion.pvuvStatics("wechat_share_click", "");
                dismiss();
                break;
            case R.id.friend:
                if (mShareBean == null) {
                    return;
                }
                mShareBean.platform = WechatMoments.NAME;
                ShareUtils.share(mActivity, mShareBean, this);
                AdPresenter.Companion.pvuvStatics("friend_share_click", "");
                dismiss();
                break;
            case R.id.face:
                Face2FaceActivity.actionStart(getContext());
                dismiss();
                AdPresenter.Companion.pvuvStatics("face_share_click", "");
                break;
            case R.id.cancel:
                dismiss();
                break;
        }
    }

    @Override
    public void dismiss() {
        if (mActivity != null && !mActivity.isFinishing()) {
            super.dismiss();
        }
    }
}
